# -*- coding: utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2JSON import Table2Json
import time

PAGE_SIZE = 3
local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

'''
姚明 281720252@qq.com
'''


class ProcessChildAPI(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections()  # 初始化数据库链接

    def queryProcessChild(self, page_index):  # zzh_prochild表查询记录,根据什么规则查询？
        cur = self.conn.cursor()
        sql = "select * from zzh_prochild"
        total_count = cur.execute(sql)
        processChildList = cur.fetchall()
        cur.close()
        self.conn.close()

        if (page_index * PAGE_SIZE) < total_count:
            start = (page_index - 1) * PAGE_SIZE
            end = page_index * PAGE_SIZE
            processChildListPart = processChildList[start: end]

            processChilsListJson = Table2Json.process_list_to_json(processChildListPart)
            return total_count, processChilsListJson
        else:
            start = (page_index - 1) * PAGE_SIZE
            processChildListPart = processChildList[start:]
            processChilsListJson = Table2Json.process_list_to_json(processChildListPart)
            return total_count, processChilsListJson

    def addProcessChild(self, addProcessChild):  # process表添加记录
        processChildName = addProcessChild['processChildName']  # processChildName  子流程的名称
        processChildCode = addProcessChild['processChildCode']  # processChildCode 子流程步数
        processChildStatus = addProcessChild['processChildStatus']  # processChildStatus  子流程状态
        cur = self.conn.cursor()
        sql = "INSERT INTO zzh_prochild (prochild_name,prochild_code,prochild_status) values (%s,%s,%s)"
        addResultOfDatabaseProcessChild = cur.execute(sql, [processChildName, processChildCode, processChildStatus])
        self.conn.commit()
        if addResultOfDatabaseProcessChild:  # 插入成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 插入失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def updateProcessChildByProcessId(self, newUpdateProcessChild, processChildId):
        processChildName = newUpdateProcessChild['processChildName']
        processChildCode = newUpdateProcessChild['processChildCode']
        processChildStatus = newUpdateProcessChild['processChildStatus']
        cur = self.conn.cursor()
        sql = "UPDATE zzh_prochild SET prochild_name=%s,prochild_code=%s,prochild_status=%s WHERE prochild_id=%s"
        updateProcessChildResultOfDatabase = cur.execute(sql, [processChildName, processChildCode, processChildStatus,
                                                               processChildId])
        self.conn.commit()
        if updateProcessChildResultOfDatabase:  # 更新成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 更新失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def deleteProcessChildById(self, processChildId):
        cur = self.conn.cursor()
        sql = "DELETE from zzh_prochild WHERE prochild_id=%s"
        deleteProcessChildResultOfDatabase = cur.execute(sql, [processChildId])
        self.conn.commit()
        if deleteProcessChildResultOfDatabase:  # 删除成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 删除失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0
